/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package helper;

import dbconnector.DBDriver;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 *
 * @author Administrator
 */
public class Login 
{
public static boolean isExists(String userid, String password)
{
     try{
         PreparedStatement p= DBDriver.getPrepare("select 1 from login where userid=? and password=?");
         p.setString(1, userid);
         p.setString(2, password);
         ResultSet rs=p.executeQuery();
         if(rs.next()) return true;
        }
        catch(Exception e)
        {}
        return false;
}

public static AO.Login getLoginBean(String userid, String password)
{
    try{
         PreparedStatement p= DBDriver.getPrepare("select * from login where userid=? and password=?");
         p.setString(1, userid);
         p.setString(2, password);
         ResultSet rs=p.executeQuery();
         if(rs.next())
         {AO.Login login=new AO.Login(rs.getString("userid"),rs.getString("password"),rs.getString("usertype"),rs.getString("email"),rs.getBoolean("isenabled"),rs.getBoolean("isloggedin"),rs.getString("joindate"));
          return login;
        }
    }
        catch(Exception e){}
        return null;
    
}

public static AO.Admin getAdminBean(String userid, String password)
{
    try{
         PreparedStatement p= DBDriver.getPrepare("select * from admin where userid=? and password=?");
         p.setString(1, userid);
         p.setString(2, password);
         ResultSet rs=p.executeQuery();
         if(rs.next())
         { 
                AO.Admin admin=new AO.Admin();
         admin.setUserid(rs.getString("userid"));
         admin.setPassword(rs.getString("password"));
          
         return admin;
         }
       }
     catch(Exception e){}return null;
    }

public static AO.Student getStudentBean(String userid)
{
    try{
         PreparedStatement p= DBDriver.getPrepare("select * from student where userid=?");
         p.setString(1, userid);
         ResultSet rs=p.executeQuery();
         if(rs.next())
         {      AO.Student student=new AO.Student();
         student.setUserid(rs.getString("userid"));
         student.setPassword(rs.getString("password"));
         student.setFname(rs.getString("fname"));
         student.setLname(rs.getString("lname"));
         student.setQualification(rs.getString("qualification"));
         student.setEmail(rs.getString("email"));
         student.setMobile(rs.getString("mobile"));
         student.setGender(rs.getString("gender"));
        
         return student;
        }
    }
        catch(Exception e){}
        return null;
    
}

public static AO.Faculty getFacultyBean(String userid)
{
    try{
         PreparedStatement p= DBDriver.getPrepare("select * from faculty where userid=?");
         p.setString(1, userid);
         
         ResultSet rs=p.executeQuery();
         if(rs.next())
         {      AO.Faculty faculty=new AO.Faculty();
         faculty.setUserid(rs.getString("userid"));
         faculty.setPassword(rs.getString("password"));
         faculty.setFname(rs.getString("fname"));
         faculty.setLname(rs.getString("lname"));
         faculty.setQualification(rs.getString("qualification"));
         faculty.setEmail(rs.getString("email"));
         faculty.setMobile(rs.getString("mobile"));
         faculty.setGender(rs.getString("gender"));
         faculty.setExperiance(rs.getString("experiance"));
         faculty.setSalary(rs.getString("salary"));
         
         return faculty;
        }
    }
        catch(Exception e){}
        return null;
    
}

public static boolean isAdmin(String userid, String password)
{
    try{
         PreparedStatement p= DBDriver.getPrepare("select 1 from admin where userid=? and password=?");
         p.setString(1, userid);
         p.setString(2, password);
         ResultSet rs=p.executeQuery();
         if(rs.next()) return true;
         
    }
        catch(Exception e){}
        return false;
    
}

public static String[] getUserids(String usertype)
{
    PreparedStatement p,pstmt;
    try{
   if(usertype.equals("users"))
    {p= DBDriver.getPrepare("select userid from login where usertype != 'admin' order by joindate desc");pstmt= DBDriver.getPrepare("select 1 from login where usertype != 'admin'"); }
    else if(usertype.equals("all"))
    {p= DBDriver.getPrepare("select userid from login  order by joindate desc");pstmt= DBDriver.getPrepare("select 1 from login");}
    else if(usertype.equals("student"))
    {p= DBDriver.getPrepare("select userid from login where usertype = 'student' order by joindate desc");pstmt= DBDriver.getPrepare("select 1 from login where usertype = 'student'");}
    else if(usertype.equals("faculty"))
    {p= DBDriver.getPrepare("select userid from login where usertype = 'faculty' order by joindate desc");pstmt= DBDriver.getPrepare("select 1 from login where usertype = 'faculty'");}
    else{return null;} 
    p.executeQuery();pstmt.executeQuery();
    
    ResultSet rs=pstmt.executeQuery();
    int i=0,j=0;
    while(rs.next()) j++;
    rs.close();
    rs=p.executeQuery();
    String[] str=new String[j];
    while(rs.next())str[i++]=rs.getString("userid");
    return str;
    }
    catch(Exception e){e.printStackTrace();return null;}

}

/*
 * public static String getType(String userid, String password) 
        
{
        try{
         PreparedStatement p= DBDriver.getPrepare("select usertype from login where userid=? and password=?");
         p.setString(1, userid);
         p.setString(2, password);
         ResultSet rs=p.executeQuery();
         if(rs.next()) 
             return rs.getString(1);
        }
        catch(Exception e){}
        return null;
    
}
*
 */
public static void main(String a[])
{
   // Login.getType("raghu", "rago");
    //System.out.print(Login.getType("raghu", "rago"));
    //System.out.println(getLoginBean("raghu","rago").getEmail());
    System.out.println(getUserids("users")[0]);
}
}
